<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportZlbtController
 * @package Admin\Controller
 */
class ReportZlbtController extends BaseController {

    public function ajax_export(){

        header("Content-type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=租赁补贴报表.xls");  //文件下载

        $info = array();
        $total = array("num"=> 0, "total"=>0);
        $this->action_GenerateReport(I("get.year"), $info, $total);

        echo '<table width="200" border="1">
                  <tr>
                    <td>补贴季度</td>
                    <td>正常发放金额</td>
                    <td>逾期发放金额</td>
                    <td>总共发放金额</td>
                  </tr>';
        foreach(array_keys($info) as $item){
            $tr = "
                <tr>
                <td>{{period}}</td>
                <td>{{normal}}</td>
                <td>{{overdue}}</td>
                <td>{{total}}</td>
              </tr>";
            echo  strtr($tr, [
                "{{period}}" => $item,
                "{{normal}}" => $info[$item]['normal']['value'],
                "{{overdue}}" => $info[$item]['overdue']['value'],
                "{{total}}" => $info[$item]['total']['value'],
            ]);
        }
        $footer = "
                <tr>
                <td></td>
                <td>{{normal}}</td>
                <td>{{overdue}}</td>
                <td>{{total}}</td>
              </tr>
            </table>";
        echo  strtr($footer, [
            "{{normal}}" => $total['normal'],
            "{{overdue}}" => $total['overdue'],
            "{{total}}" => $total['total'],
        ]);
    }

    private function action_GenerateReport($year, &$info, &$total){

        $where = array(
            "period" => array('like', $year."%")
        );
        $season = array(
            array("name"=> $year."年第1季度", "code"=>$year."01"),
            array("name"=> $year."年第2季度", "code"=>$year."02"),
            array("name"=> $year."年第3季度", "code"=>$year."03"),
            array("name"=> $year."年第4季度", "code"=>$year."04"),
        );
        foreach($season as $item){
            $info[$item['code']] = array(
                "code"=>$item['code'],
                "name"=>$item['name'],
                'normal' => array("value"=>0.00, "where"=>serialize(array("period"=>$item['code'],"type"=>'正常发放'))),
                'overdue' => array("value"=>0.00, "where"=>serialize(array("period"=>$item['code'],"type"=>'正常发放'))),
                'total' => array("value"=>0.00, "where"=>serialize(array("period"=>$item['code']))),
            );
        }

        $data = M("butie")->field("period, type, sum(amount) as total")
            ->where($where)->group("period, type")->select();
        foreach ($data as $item) {
            if($item['type']=='正常发放'){
                $info[$item['period']]["normal"]['value'] += $item['total'];
                $total['normal'] += $item['total'];
            }else if($item['type']=='逾期补发'){
                $info[$item['period']]["overdue"]['value'] += $item['total'];
                $total['overdue'] += $item['total'];
            }
            $info[$item['period']]["total"]['value'] += $item['total'];
            $total['total'] += $item['total'];
        }
    }

    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=-1; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){
        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);
        $year = I("request.year")?I("request.year"):date("Y");
        $this->assign("year", $year);

        $info = array();
        $total = array("normal"=> 0, "overdue"=>0, "total"=>0);
        $this->action_GenerateReport($year, $info, $total);
        $this->assign("total", $total);
        $this->assign("zlbt", $info);

        $this->display();
    }


}